<?php

namespace App\Models\Kms\Album;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class AlbumMainModel extends BaseModel
{
    use HasFactory;

    const TYPE_0 = "个人相册";
    const TYPE_1 = "团队相册";
    const TYPE_2 = "门店相册";

    protected $table = "kms_album_main";

    protected $guarded = [];

    public $timestamps = false;

    /**
     * @param mixed $value
     * @return false|Carbon|mixed
     */
    protected function asDateTime($value)
    {
        return $value;
    }


    /**
     * @param int $uid
     * @return Builder|Model
     */
    public static function detail(int $uid)
    {
        return self::query()->firstOrCreate(
            ["ownerUid" => $uid, "type" => self::TYPE_0],
            [
                "resourceLastUpdateTime" => 0,
                "createdAt"              => time(),
                "updatedAt"              => time(),
                "createdByUid"           => $uid,
                "updatedByUid"           => $uid
            ]
        );
    }

    /**
     * @param int $time
     * @param int $mid
     */
    public static function setLastUpdateTime(int $time, int $mid)
    {
        self::query()->where("id", $mid)->update([
            "resourceLastUpdateTime" => $time,
            "updatedAt"              => $time,
            "updatedByUid"           => request()->user()->rmsUserId
        ]);
    }
}
